package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import net.bytebuddy.asm.Advice;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebRotationEntity;
import io.renren.modules.generator.service.WebRotationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 首页轮播图表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 11:07:46
 */
@RestController
@RequestMapping("generator/webrotation")
@Api(value = "首页轮播图",tags = {"首页轮播图"})
public class WebRotationController {

    @Autowired
    private WebRotationService webRotationService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询最大排序码
     */
    @RequestMapping("/getMaxSort")
    @RequiresPermissions("generator:webrotation:list")
    public Integer getMaxSort(){
        Integer maxSort = webRotationService.getMaxSort();

        return maxSort==null?0:maxSort;
    }
    /**
     * 获取轮播图
     */
    @RequestMapping("/getSideList")
//    @RequiresPermissions("generator:webrotation:list")
    public R getSideList(){
          List<WebRotationEntity> sideList=this.webRotationService.getSideList();
//        List<WebRotationEntity> sideList = webRotationService.list();
        return R.ok().put("sideList", sideList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("generator:webrotation:list")
    public R list(@RequestParam Map<String, Object> params){
       String key= (String) params.get("key");

        PageUtils page = webRotationService.queryPage(params);
        List<WebRotationEntity> list = (List<WebRotationEntity>) page.getList();
        for (WebRotationEntity webRotationEntity : list) {
            Integer createUser = webRotationEntity.getCreateUser();
            SysUserEntity sysUserServiceById = sysUserService.getById(createUser);
            webRotationEntity.setUserName(sysUserServiceById.getUsername());
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rotationId}")
    @RequiresPermissions("generator:webrotation:info")
    public R info(@PathVariable("rotationId") Integer rotationId){
		WebRotationEntity webRotation = webRotationService.getById(rotationId);

        return R.ok().put("webRotation", webRotation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:webrotation:save")
    public R save(@RequestBody WebRotationEntity webRotation){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
        webRotation.setCreateUser(Math.toIntExact(userId));
        webRotation.setCreateTime(new Date());
        webRotation.setUpdateTime(new Date());
        webRotationService.save(webRotation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:webrotation:update")
    public R update(@RequestBody WebRotationEntity webRotation){
        webRotation.setUpdateTime(new Date());
		webRotationService.updateById(webRotation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:webrotation:delete")
    public R delete(@RequestBody Integer[] rotationIds){
		webRotationService.removeByIds(Arrays.asList(rotationIds));

        return R.ok();
    }

}
